home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October: Mac OS SDK / Dev.CD Oct 97 SDK1.toast / Development Kits (Disc 1) / Multiprocessing SDK / Sample Code / MPHelloWorld / sources / MPHelloWorld.c next >
Encoding:
C/C++ Source or Header  |  1996-09-17  |  3.8 KB  |  133 lines  |  [TEXT/CWIE]

  1. #include <stdio.h>
  2. #include <Types.h>
  3. #include <SIOUX.h>
  4. #include <MP.h>
  5.  
  6.  
  7. static void sendString(MPQueueID queue, char *string) {
  8. /*
  9.   Send the string through the queue one character at a time.
  10. */
  11.   while (*string) {
  12.     (void) MPNotifyQueue(queue, (void *) *string, 0, 0);
  13.     string++;
  14.   }
  15.  
  16.   (void) MPNotifyQueue(queue, (void *) '\0', 0, 0);            /* Send the final null character. */
  17. }
  18.  
  19.  
  20. static void
  21. receiveString(MPQueueID queue, char *string) {
  22. /*
  23.   Reassemble a string being transferred over the queue and return it to the caller.
  24. */
  25.   void
  26.     *first,            /* The first word of the message. */
  27.     *second,        /* The second word of the message. */
  28.     *third;            /* The third word of the message. */
  29.  
  30.   for (;;) {
  31.     (void) MPWaitOnQueue(queue, &first, &second, &third, kDurationForever);
  32.     *string = (char) first;
  33.  
  34.     if (*string == '\0') {
  35.       return;
  36.     }
  37.  
  38.     string++;
  39.   }
  40. }
  41.  
  42.  
  43. static OSStatus HelloWorld(void *parameter) {
  44.   MPQueueID queue = parameter;
  45.  
  46.   sendString(queue, "Hello, World!");
  47.  
  48.   return noErr;
  49. }
  50.  
  51.  
  52. static int
  53. failure(char *annotation, char *routine, OSStatus status) {
  54.   printf("Uh oh.  %s%s%s returned %s [%d].\n",
  55.          (annotation) ? annotation : "",
  56.          (routine) ? routine : "",
  57.          (routine) ? "()" : "A routine",
  58.          _MPStatusCString(status),
  59.          status);
  60.  
  61.   return 1;
  62. }
  63.  
  64.  
  65. int
  66. main(void) {
  67.   OSStatus
  68.     status;                    /* We'll use this to test the outcome of each MP function. */
  69.   MPQueueID
  70.     terminationQueue,        /* This queue will report the completion of the task. */
  71.     communicationQueue;        /* This queue will be used to communicate between the app and the task. */
  72.   MPTaskID
  73.     task;                    /* This will be the ID of the task we create. */
  74.   char
  75.     myString[100];
  76.  
  77.   SIOUXSettings.asktosaveonclose = 0;
  78.  
  79.   if (!MPLibraryIsLoaded()) {
  80.     printf("Can't run without the \"%s\" shared library.\n", MPLibraryName);
  81.  
  82.     return 1;
  83.   }
  84.  
  85.   status = MPCreateQueue(&terminationQueue);    /* Create the queue which will report the completion of the task. */
  86.   if (status != noErr) {
  87.     return failure("Cannot create the termination queue:\n    ", "MPCreateQueue", status);
  88.   }
  89.  
  90.   status = MPCreateQueue(&communicationQueue);    /* Create the queue we'll use to communicate with. */
  91.   if (status != noErr) {
  92.     (void) MPDeleteQueue(terminationQueue);
  93.  
  94.     return failure("Cannot create the communication queue:\n    ", "MPCreateQueue", status);
  95.   }
  96.  
  97.   status = MPCreateTask(HelloWorld,                /* This is the task function. */
  98.                         communicationQueue,        /* This is the parameter to the task function. */
  99.                         kMPUseDefaultStackSize,    /* We'll use whatever the MP system software gives us. */
  100.                         terminationQueue,        /* We'll use this to sense task completion. */
  101.                         0,                        /* We won't use the first part of the termination message. */
  102.                         0,                        /* We won't use the second part of the termination message. */
  103.                         kMPNormalTaskOptions,    /* Use the normal task options. (Currently this *must* be zero!) */
  104.                         &task);                    /* Here's where the ID of the new task will go. */
  105.   if (status != noErr) {
  106.     (void) MPDeleteQueue(terminationQueue);
  107.     (void) MPDeleteQueue(communicationQueue);
  108.  
  109.     return failure(0, "MPCreateTask", status);
  110.   }
  111.  
  112.   receiveString(communicationQueue, myString);
  113.   printf("%s\n", myString);
  114.  
  115.   /* Wait for the task to complete: */
  116.   status = MPWaitOnQueue(terminationQueue, 0, 0, 0, kDurationForever);
  117.   if (status != noErr) {
  118.     (void) failure("While waiting for task completion:\n    ", "MPWaitOnQueue", status);
  119.   }
  120.  
  121.   status = MPDeleteQueue(terminationQueue);
  122.   if (status != noErr) {
  123.     (void) failure("Can't delete the termination queue:\n    ", "MPDeleteQueue", status);
  124.   }
  125.  
  126.   status = MPDeleteQueue(communicationQueue);
  127.   if (status != noErr) {
  128.     (void) failure("Can't delete the communication queue:\n    ", "MPDeleteQueue", status);
  129.   }
  130.  
  131.   return 0;
  132. }
  133.